package com.seafwg.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seafwg.eduservice.entity.EduChapter;
import com.seafwg.eduservice.entity.EduVideo;
import com.seafwg.eduservice.entity.chapter.ChapterVo;
import com.seafwg.eduservice.entity.chapter.VideoVo;
import com.seafwg.eduservice.mapper.EduChapterMapper;
import com.seafwg.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seafwg.eduservice.service.EduVideoService;
import com.seafwg.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author seafwg
 * @since 2021-01-19
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    /**
     * 课程大纲列表,根据课程id进行查询
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 1.根据courseId查询课程里所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        // 2.根据courseId查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        // 要操作小节表注入EduVideoService
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        // 创建最终集合，封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        // 3.进行最终的数据封装ChapterVo
        for (int i = 0; i < eduChapterList.size(); i++) {
            //获取每一个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //封装ChapterVo,并添加到最终的集合中
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);

            // 创建小节对象
            List<VideoVo> videoList = new ArrayList<>();

            // 遍历小节对象，封装到小节对象的集合中
            for (int j = 0; j < eduVideoList.size(); j++) {
                // 获取小节
                EduVideo eduVideo = eduVideoList.get(j);
                // 小节chapterId和章节id是否一样
                if(eduVideo.getChapterId().equals(eduChapter.getId())) {
                    // 如果相同进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoList);
        }
        return finalList;
    }

    /**
     * 根据章节id删除章节信息:
     * 删除出逻辑：通过id查询章节，查出章节的小节，判断如果有小节不能删除，
     * @param chapterId
     * @return
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //构造查询条件
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        if(count > 0) { // 查出小节，有小节不删除
            throw new MyException(20001, "有小节不删除");
        }else {
            // 删除章节：
            int res = baseMapper.deleteById(chapterId);
            // 1>0(true); 0>0(false)
            return res > 0;
        }
    }
}
